package com.neusoft.neuiotms.dcvm.service.impl;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcvm.converter.ConfigConverter;
import com.neusoft.neuiotms.dcvm.dto.ConfigDTO;
import com.neusoft.neuiotms.dcvm.model.Config;
import com.neusoft.neuiotms.dcvm.repository.ConfigRepository;
import com.neusoft.neuiotms.dcvm.service.ConfigService;

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigRepository configRepository;
    @Autowired
    private ConfigConverter configConverter;

    @Override
    public ConfigDTO details(Long id) {
        Config model;
        try {
            model = this.configRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.configConverter.toDTO(model);
    }

    @Override
    public ConfigDTO details(String code) {
        final Config model = this.configRepository.findOneByCode(code);
        if (null == model) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {code });
        }
        return this.configConverter.toDTO(model);
    }

    @Override
    public ConfigDTO create(ConfigDTO dto) {
        Config model = this.configRepository.findOneByCode(dto.getCode());
        if (null == model) {
            model = this.configConverter.toModel(dto);
        } else {
            dto.setId(model.getId());
            model = this.configConverter.toModel(dto);
        }

        this.configRepository.save(model);
        return this.configConverter.toDTO(model);
    }

    @Override
    public ConfigDTO update(Long id, ConfigDTO dto) {
        Config model;
        try {
            model = this.configRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.configConverter.toModel(dto);
        this.configRepository.save(model);

        return this.configConverter.toDTO(model);
    }

    @Override
    public void delete(Long id) {
        try {
            this.configRepository.deleteById(id);
        } catch (final EmptyResultDataAccessException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

    }

    @Override
    public Page<ConfigDTO> search(Pageable pageable, Searchable searchable) {
        final Page<Config> models = this.configRepository.search(pageable, searchable);
        return this.configConverter.toPageDTO(models);
    }

}
